
window.utils = {

    // 随机数
    random:function(min, max) { 
        return Math.floor(Math.random() * (max - min)) + min
    },

    /*
    * 求圆周上等分点的坐标
    * ox,oy为圆心坐标
    * r为半径
    * count为等分个数
    */
    getPoint(r, ox, oy, count){
        var point = []; //结果
        var radians = (Math.PI / 180) * Math.round(360 / count);//弧度
        for(var i = 0; i < count; i++){
            var x = ox + r * Math.sin(radians * i)
            var y = oy + r * Math.cos(radians * i)
            point.unshift({x:x,y:y}); //为保持数据顺时针
        }
        return point;
    },

    // 两点之间的距离
    distance:function(p1, p2) { 
        var dx = Math.abs(p2.x - p1.x);
        var dy = Math.abs(p2.y - p1.y);
        return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
    },

    /**
     * 获取当前节点转换到某节点下的坐标
     * @param {*} curNode  当前节点
     * @param {*} targetNode 目标节点 
     */
    getNodePos: function (curNode,targetNode) {
        var worldPos = curNode.parent.convertToWorldSpaceAR(curNode.getPosition())
        var pos = targetNode.convertToNodeSpaceAR(worldPos)
        return pos;
    },
   

    // 货币进位
    goldCrarryBit:function(gold) { 
        let arr = [
            [1000000000000, 'T'],
            [1000000000, 'B'],
            [1000000, 'M'],
            [1000, 'k']
        ]
      
        for (const v of arr) {
            let value = gold / v[0]
            if (value > 1) return value.toFixed(1) + v[1]
        }
        return gold
    },


    //节点动态修改精灵图片
    setSpriteFrame (targetNode, path) {
        cc.resources.load(path, cc.SpriteFrame, (err, asset) => {
            targetNode.getComponent(cc.Sprite).spriteFrame = asset;
        });
    },


    //获取某个图集中某种图片
    setSpriteAtlas(targetNode, atlasPath, spriteName){
        cc.resources.load(atlasPath, cc.SpriteAtlas, function (err, atlas) {
            var frame = atlas.getSpriteFrame(spriteName);
            targetNode.getComponent(cc.Sprite).spriteFrame = frame
            // targetNode.spriteFrame = frame;
        });
    },



    shakeEffect: function (currNode) {
        currNode.runAction(
            cc.sequence(
                cc.moveTo(0.02, cc.Vec2(1, 3)),
                cc.moveTo(0.02, cc.Vec2(2, -2)),
                cc.moveTo(0.02, cc.Vec2(-1, 3)),
                cc.moveTo(0.02, cc.Vec2(2, -3)),
                cc.moveTo(0.02, cc.Vec2(-1, 3)),
                cc.moveTo(0.02, cc.Vec2(2, -2)),
                cc.moveTo(0.02, cc.Vec2(-2, 3)),
                cc.moveTo(0.02, cc.Vec2(-3, 2)),
                cc.moveTo(0.02, cc.Vec2(0, 0))
            )
        );
    },
    

};





